﻿/*
 * @Autor: PK
 * @Date: 2022/01/12 17:17:SS
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ShkSimulation.desktop.setting.model {

	public class ModelValidateCheck {

		
		public static void CheckUnique(ICollection<StationConfigModel> stationIds, ICollection<RoadConfigModel> roadIds) {
			ISet<int> ids = new HashSet<int>();
			foreach (var stationConfigModel in stationIds) {
				if (ids.Contains(stationConfigModel.Id)) {
					int random = Guid.NewGuid().GetHashCode();
					string message = $"station的id不能重复{stationConfigModel.Id}，已更改为随机值:{random}";
					MessageBox.Show(message, @"提示");
					stationConfigModel.Id = random;
				}
				ids.Add(stationConfigModel.Id);
			}
			foreach (var roadConfigModel in roadIds) {
				if (ids.Contains(roadConfigModel.Id)) {
					int random = Guid.NewGuid().GetHashCode();
					string message = $"station和road的id不能重复{roadConfigModel.Id}，已更改为随机值:{random}";
					MessageBox.Show(message, @"提示");
					roadConfigModel.Id = random;
				}
				ids.Add(roadConfigModel.Id);
			}
		}


		public static bool CheckValidate(DataTable stationTable, 
			DataTable roadTable, 
			DataTable demandTable, out string message) {
			bool sucess;
			var (stations, roads, demands) = ConverUtil.DataTableToConfigModels(stationTable, roadTable, demandTable, out sucess);
			if (!sucess) {
				message = $"转换失败啦，请检查数据";
				return false;
			}
			ISet<int> ids = new HashSet<int>();
			foreach (var station in stations) {
				if (ids.Contains(station.Id)) {
					message = $"站台id重复啦：{station.Id}";
					return false;
				}
				ids.Add(station.Id);
			}
			ISet<int> stationIds = new HashSet<int>(ids);
			foreach (var road in roads) {
				if (ids.Contains(road.Id)) {
					message = $"道路id重复啦：{road.Id}";
					return false;
				}
				ids.Add(road.Id);
			}
			foreach (var demand in demands) {
				if (!stationIds.Contains(demand.OriginId) || !stationIds.Contains(demand.DestinationId)) {
					message = $"不存在的需求起终点：{demand.OriginId}:{demand.DestinationId}";
					return false;
				}
			}
			message = "OK";
			return true;
		}

	}

}